package mta.command.patch;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class PatchBuilderOptions {
    private String[] args;
    private CommandLine cl;
    private PatchAffect affect;

    public enum PatchAffect {
        All, Untranslated
    }

    public PatchBuilderOptions(String[] args) {
        this.args = args;
    }

    private PatchAffect parseTypeOfAffetcedPhrases() throws ParseException {
        int count = 0;
        PatchAffect result = null;

        if (cl.hasOption("a")) {
            result = PatchAffect.All;
            count++;
        }

        if (cl.hasOption("u")) {
            result = PatchAffect.Untranslated;
            count++;
        }

        if (count == 1) {
            return result;
        } else {
            throw new ParseException("Choose type of affected phrases (all or untranslated)");
        }
    }

    public void parse() throws ParseException {

        CommandLineParser parser = new PosixParser();
        cl = parser.parse(new PatchOptions().getOptions(), args);

        if (!cl.hasOption("f") || !cl.hasOption("l")) {
            throw new ParseException("You need specify patch & localization destinations");
        }

        affect = parseTypeOfAffetcedPhrases();
    }

    public String getFilePath() {
        return cl.getOptionValue("f");
    }

    public String getLocalizationPath() {
        return cl.getOptionValue("l");
    }

    public PatchAffect getTypeOfAffectedPhrases() {
        return affect;
    }
}